package com.product.system.cwebsocket;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.product.system.service.SysRoleService;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.goeasy.GoEasy;
import io.goeasy.publish.GoEasyError;
import io.goeasy.publish.PublishListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class GoEasySendApi {


    @Autowired
    private SysRoleService sysRoleService;

    @Value("${goeasy.commonkey}")
    private String commonKey;
    /**
     * 3秒执行一次
     * @return
     * @throws Exception
     */
//    @Scheduled(fixedRate = 500 * 1000)
//    @XxlJob("MyWebSocket")
//    public void send() throws Exception {
//        Map<String,Object> map = new HashMap<>();
//        map.put("server",sysRoleService.showForm());
//        map.put("pie",sysRoleService.showForm());
//        JSONObject jsonObject =  new JSONObject(map);
//        WebSocketServer.sendAllMessage(jsonObject.toString());
//        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
//
//    }



//    @Scheduled(fixedRate = 3 * 1000)
    @XxlJob("MyWebSocket")
    public void send() throws Exception {

        Map<String,Object> map = new HashMap<>();
        map.put("server",sysRoleService.showForm());
        map.put("pie",sysRoleService.showForm());
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(map);

        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        //新加坡rest-host：rest-singapore.goeasy.io
        GoEasy goEasy = new GoEasy("https://rest-hz.goeasy.io", commonKey);
        goEasy.publish("GoEasyealTimeData_0828", jsonString, new PublishListener(){
            @Override
            public void onSuccess() {
                System.out.println("Publish success.");
            }

            @Override
            public void onFailed(GoEasyError error) {
                System.out.println("Failed to Publish message, error:" + error.getCode() + " , " + error.getContent());
            }
        });

    }







}